package com;

import java.awt.*;
import java.awt.event.*;
import java.sql.DriverManager;
import javax.swing.JOptionPane;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Properties;


public class EmpUI implements ActionListener{
	/*Linux JDBC related code start*/
	private static final String dbClassName = "com.mysql.jdbc.Driver";
	private static final String CONNECTION = "jdbc:mysql://127.0.0.1/test";
	/*Linux JDBC related code end*/
	Frame f;
	Button b1;
	Label l1, l2, l3, l4;
	TextField t1, t2, t3;
	GridLayout g1;

	public EmpUI() {
			    
		f= new Frame("Insert");
		g1 = new GridLayout(4,2);
		f.setLayout(g1);
		l1 = new Label ("Enter the Employee name");
		l2 = new Label ("Enter the Employee Address");
		l3 = new Label ("Enter the Employee salary");
		l4 = new Label ("");

		t1 = new TextField();
		t2 = new TextField();
		t3 = new TextField();

		b1 = new Button("Insert");

		f.add(l1);
		f.add(t1);
		f.add(l2);
		f.add(t2);
		f.add(l3);
		f.add(t3);
		f.add(b1);
		//f.add(l4);

		b1.addActionListener(this);

		f.setVisible(true);
		f.setSize(500,400);
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
	}

	public void actionPerformed(ActionEvent e){
		try{
			/*Linux JDBC related code start*/
			Properties p = new Properties();
		    p.put("user","root");
		    p.put("password","dkit");/*Linux JDBC related code end*/
		    
			Class.forName(dbClassName);
			Connection con = DriverManager.getConnection(CONNECTION,p);
			String query = "Insert into emp (ename, eadd, esal) values (?, ?, ?)";
			PreparedStatement pd = con.prepareStatement(query);
			pd.setString(1,t1.getText());
			pd.setString(2,t2.getText());
			pd.setInt(3,Integer.parseInt(t3.getText()));
			//l4.setText("You have registered with us");
			int i = pd.executeUpdate();
			if(i>0){
				JOptionPane.showMessageDialog(f, "Your details has been inserted");
			}
			con.close();
		}
		catch (Exception ex){
			JOptionPane.showMessageDialog(f, "Try again");
		}
	}

	public static void main(String[] args) {
		new EmpUI();
	}

}
